package interfaces

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"ruoyi/application"
	"ruoyi/utils"
	"strings"
)

type Captcha struct {
	captcha application.ConfigAppInterface
}

//captcha constructor
func NewCaptcha(captcha application.ConfigAppInterface) *Captcha {
	return &Captcha{
		captcha: captcha,
	}
}

// CaptchaImageHandler
// @Tags      Base
// @Summary   生成验证码
// @accept    application/json
// @Produce   application/json
// @Success   200  {object}  response.Response{data=response.SysCaptchaResponse,msg=string}  "生成验证码,返回包括随机数id,base64,验证码长度,是否开启验证码"
//// @Router    /captchaImage [get]
func (fo *Captcha) CaptchaImageHandler(context *gin.Context) {
	var captchaEnabled = fo.captcha.SelectCaptchaEnabled()

	id, b64s, err := utils.CreateImageCaptcha()
	if err != nil {
		context.JSON(http.StatusOK, gin.H{
			"msg":  "生产二维码失败，请联系管理员",
			"code": http.StatusInternalServerError,
		})
		return
	}

	context.JSON(http.StatusOK, gin.H{
		"msg":            "操作成功",
		"img":            strings.ReplaceAll(b64s, "data:image/png;base64,", ""),
		"code":           http.StatusOK,
		"captchaEnabled": captchaEnabled,
		"uuid":           id,
	})
	return
}
